﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_MvvmLight.Base;

namespace WpfApp_MvvmLight.ViewModel;

public class MainViewModel : ViewModelBase //ObservableObject
{
    private string? _value;

    public string? Value
    {
        get { return _value; }
        set
        {
            //_value = value;
            //RaisePropertyChanged();

            Set<string?>(ref _value, value);
            BtnCommand2?.RaiseCanExecuteChanged();
        }
    }


    public RelayCommand? BtnCommand1 { get; set; }
    public RelayCommand<string?>? BtnCommand2 { get; set; }


    public MainViewModel()
    {
        if (IsInDesignMode) //设计模式下显示111
            Value = "111";
        else
            Value = "222";

        BtnCommand1 = new RelayCommand(() =>
        {
            Messenger.Default.Send<WindowCom>(new WindowCom 
            {
                Value = "123",
                SubmitAction = _accept => 
                {
                    
                }
            });
        },
        () =>
        {
            return true;
        });

        BtnCommand2 = new RelayCommand<string?>(arg =>
        {
            Messenger.Default.Send<string>("");

            //跨线程调用
            DispatcherHelper.CheckBeginInvokeOnUI(() => 
            {
                
            });
        },
        arg =>
        {
            return Value == arg;
        });

    }

    /// <summary>
    /// 清理方法，清理一下监听、消息等 通过 ViewModelBase 基类多态进行调用
    /// </summary>
    public override void Cleanup()
    {
        base.Cleanup(); 
    }
}
